<?php
// ========================== 文件说明 ==========================//
// 本文件说明：归档文章浏览
// --------------------------------------------------------------//
// 本程序作者：angel
// --------------------------------------------------------------//
// 本程序版本：SaBlog-X Ver 1.6
// --------------------------------------------------------------//
// 本程序主页：http://www.sablog.net
// ========================== 开发环境 ==========================//
// register_globals = Off
// --------------------------------------------------------------//
// magic_quotes_gpc = On
// --------------------------------------------------------------//
// safe_mode = On
// --------------------------------------------------------------//
// Windows server 2003 & Linux & FreeBSD
// --------------------------------------------------------------//
// Apache/1.3.33 & PHP/4.3.2 & MySQL/4.0.17
// --------------------------------------------------------------//
// Apache/1.3.34 & PHP/4.4.1 & MySQL/5.0.16
// --------------------------------------------------------------//
// Apache/2.0.55 & PHP/5.1.1 & MySQL/5.0.15
// --------------------------------------------------------------//
// Copyright (C) Security Angel Team All Rights Reserved.
// ==============================================================//

if(!defined('IN_SABLOG')) {
	exit('Access Denied');
}

$articleid = intval($article);
// 获取文章信息	
if (!$articleid) {
	exit('缺少参数');
} else {
	$article = $DB->fetch_one_array("SELECT a.articleid,a.cid,a.uid,a.title,a.content,a.keywords,a.dateline,a.comments,a.attachments,a.trackbacks,a.readpassword,a.alias,c.name as cname,c.url as curl,u.username 
		FROM {$db_prefix}articles a 
		LEFT JOIN {$db_prefix}categories c ON c.cid=a.cid
		LEFT JOIN {$db_prefix}users u ON a.uid=u.userid
		WHERE a.visible='1' AND articleid='$articleid'");

	if (!$article) {
		exit('记录不存在');
	}
}

//设置文章的分类名、作者、TAG、标题成为meta\title信息
$options['meta_keywords'] = ($article['keywords'] ? $article['keywords'].',' : '').$options['meta_keywords'];
$options['meta_description'] = $article['title'].','.($article['keywords'] ? $article['keywords'].',' : '').$options['meta_description'];
$options['title_keywords'] = $options['name'].' - '.$options['title_keywords'];

sa_header();

if ($article['readpassword']) {
?>
	<div style="padding:20px;">这篇日志被加密了,<a href="$article[url]">请返回查看完整版本.</a></div>
<?php
} else {

	//记录浏览过的文章
	if(!$_COOKIE['articleids'][$article['articleid']]){
		scookie('articleids['.$article['articleid'].']', $article['articleid']);
	}

	$DB->unbuffered_query("UPDATE {$db_prefix}articles SET views=views+1 WHERE articleid='$articleid'");

	$article['url'] = getpermalink($article['articleid'], $article['alias'], 0, sadate('Y/m/d',$article['dateline']));
	$article['cateurl'] = getcatelink($article['cid'], $article['curl']);
	$article['dateline'] = sadate('Y-m-d H:i',$article['dateline']);

?>
<h1><a href="<?php echo $article['url'];?>"><?php echo $article['title'];?></a></h1>
<ul id="postinfos">
<li>分类: <a href="<?php echo $article['cateurl'];?>"><?php echo $article['cname'];?></a></li>
<li>作者: <?php echo $article['username'];?></li>
<li>时间: <?php echo $article['dateline'];?></li>
</ul>
<?php

	if (preg_match("/\[php\].+?\[\/php\]/is", $article['content'])) {
		$article['content'] = preg_replace("/\s*\[php\](.+?)\[\/php\]\s*/ies", "phphighlite('\\1')", $article['content']);
	}

?>
<div class="post"><?php echo $article['content'];?></div>
<?php

	//附件
	if ($article['attachments']) {
		require_once(SABLOG_ROOT.'include/func/attachment.func.php');
		$attachs= unserialize(sax_stripslashes($article['attachments']));
		if (is_array($attachs)) {
			foreach ($attachs AS $attach) {
				$a_path = $options['attachments_dir'].$attach['filepath'];
				if (file_exists(SABLOG_ROOT.$a_path)) {
					$a_ext = strtolower(getextension($attach['filename']));
					if ($a_ext == 'gif' || $a_ext == 'jpg' || $a_ext == 'jpeg' || $a_ext == 'png') {
						$imagesize = getimagesize(SABLOG_ROOT.$a_path);
						$a_size = sizecount($attach['filesize']);
						$a_thumb_path = $options['attachments_dir'].$attach['thumb_filepath'];
						if ($attach['thumb_filepath'] && $options['attachments_thumbs'] && file_exists(SABLOG_ROOT.$a_thumb_path)) {
							$article['image'][$attach['attachmentid']]=array($attach['attachmentid'],$a_thumb_path,$a_size,$attach['thumb_width'],$attach['thumb_height'],$attach['downloads'],1,$attach['filename']);
						} else {
							// 如果缩略图不存在
							$size = explode('x', strtolower($options['attachments_thumbs_size']));
							$im = scale_image( array(
								'max_width'  => $size[0],
								'max_height' => $size[1],
								'cur_width'  => $imagesize[0],
								'cur_height' => $imagesize[1]
							));
							$article['image'][$attach['attachmentid']]=array($attach['attachmentid'],$a_path,$a_size,$im['img_width'],$im['img_height'],$attach['downloads'],0,$attach['filename']);
						}
					} else {
						// 如果非图片文件
						$a_size = sizecount($attach['filesize']);	
						$article['file'][$attach['attachmentid']]=array($attach['attachmentid'],$attach['filename'],$a_size,$attach['downloads']);
					}
				}
			}
			//如果空,释放掉变量
			$attachmentids=array();

			$article['content'] = preg_replace("/\[attach=(\d+)\]/ie", "upload('\\1')", $article['content']);

			foreach($attachmentids as $key => $value){
				if($article['image'][$value]){
					unset($article['image'][$value]);
				}
				if($article['file'][$value]){
					unset($article['file'][$value]);
				}
			}
			if ($article['image'] && is_array($article['image'])) {
				foreach ($article['image'] as $image) {
					if ($image[6]) {
						echo '<div class="attach">图片附件(缩略图):<br /><a href="$options[url]attachment.php?id='.$image[0].'" target="_blank"><img src="'.$options['url'].$image[1].'" border="0" alt="'.$image[7].'&#13;&#13;大小: '.$image[2].'&#13;尺寸: '.$image[3].' x '.$image[4].'&#13;浏览: '.$image[5].' 次&#13;点击打开新窗口浏览全图" width="'.$image[3].'" height="'.$image[4].'" /></a></div>';
					} else {
						echo '<div class="attach">图片附件:<br /><a href="'.$options['url'].'attachment.php?id='.$image[0].'" target="_blank"><img src="'.$options['url'].$image[1].'" border="0" alt="'.$image[7].'&#13;&#13;大小: '.$image[2].'&#13;尺寸: '.$image[3].' x '.$image[4].'&#13;浏览: '.$image[5].' 次&#13;点击打开新窗口浏览全图" width="'.$image[3].'" height="'.$image[4].'" /></a></div>';
					}
				}
			}

			if ($article['file'] && is_array($article['file'])) {
				foreach ($article['file'] as $file) {
					echo '<div class="attach">附件:<a title="'.$file[1].'" href="'.$options['url'].'attachment.php?id='.$file[0].'" target="_blank">'.$file[1].'</a> ('.$file[2].', 下载次数:'.$file[3].')</div>';
				}
			}
		}
	}
	// 获取附件结束

	//TAGS
	if ($article['keywords']) {
		$tagdb = explode(',', $article['keywords']);
		$articletags = $tmark = '';
		foreach($tagdb as $tag) {
			$tag = trim($tag);
			$articletags .= $tmark.'<a href="'.gettaglink(urlencode($tag)).'">'.htmlspecialchars($tag).'</a>';
			$tmark = ', ';
		}
		echo '<h2 class="art-tag">Tags: '.$articletags.'</h2>';
	}

	// Trackback
	if ($options['enable_trackback'] && $article['trackbacks']) {
		$tborderid = 0;
		$tborder = $options['trackback_order'] ? 'ASC' : 'DESC';
		$query = $DB->query("SELECT title,dateline,excerpt,url,blog_name FROM {$db_prefix}trackbacks WHERE visible='1' AND articleid='$articleid' ORDER BY trackbackid ".$tborder);
		$trackbackdb=array();
		while ($trackback = $DB->fetch_array($query)) {
			$tborderid++;
			$trackback['tborderid'] = $tborderid;
			$trackback['url'] = sax_stripslashes($trackback['url']);
			$trackback['title'] = sax_stripslashes($trackback['title']);
			$trackback['blog_name'] = sax_stripslashes($trackback['blog_name']);
			$trackback['excerpt'] = sax_stripslashes($trackback['excerpt']);
			$trackback['dateline'] = sadate('Y-m-d H:i', $trackback['dateline']);
			$trackbackdb[] = $trackback;
		}
		unset($trackback);
		$DB->free_result($query);
		echo '<h3>Trackbacks</h3>';
		echo '<ol class="comments">';
		foreach($trackbackdb as $key => $trackback){
?>
		<li>
			<span><a href="<?php echo $trackback['url'];?>" target="_blank"><?php echo $trackback['blog_name'];?></a> - <?php echo $trackback['dateline'];?></span>
			<div class="contents"><?php echo $trackback['excerpt'];?></div>
		</li>
<?php
		}
		echo '</ol>';
	}
			
	// 评论
	if ($article['comments']) {
		$cmtorder = $options['comment_order'] ? 'ASC' : 'DESC';
		$query = $DB->query("SELECT author,url,dateline,content FROM {$db_prefix}comments WHERE articleid='$articleid' AND visible='1' ORDER BY commentid ".$cmtorder);
		$commentdb=array();
		while ($comment=$DB->fetch_array($query)) {
			$cmtorderid++;
			$comment['cmtorderid'] = $cmtorderid;
			if ($comment['url']) {
				$comment['author'] = '<a href="'.$comment['url'].'" target="_blank">'.$comment['author'].'</a>';
			}
			$comment['content'] = html_clean($comment['content']);
			$comment['dateline'] = sadate('Y-m-d H:i', $comment['dateline']);
			$commentdb[]=$comment;
		}
		unset($comment);
		$DB->free_result($query);
		echo '<h3>Comments</h3>';
		echo '<ol class="comments">';
		foreach($commentdb as $key => $comment){
?>
		<li>
			<span><?php echo $comment['author'];?> - <?php echo $comment['dateline'];?></span>
			<div class="contents"><?php echo $comment['content'];?></div>
		</li>
<?php
		}
		echo '</ol>';
	}
}

$options['title'] = $article['title'];
?>